const { formatDate } = require('../../js/tool');
let tool = require('../../js/tool');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 骨架屏
    loading: false,

    // 当日的记账数据
    currentBookingData: [],

    // 当前时间
    currentDate: '',

    // 显示X月X日
    date: '',

    // 当天收入和支出的总额
    currentDateMoney: {
      sr: 0,
      zc: 0
    },

    // 选择日期
    pickerDate: {
      start: '',
      end: ''
    },

    // 本月总额(收入-支出-结余)
    currentMonthBooking: {
      sr: 0,
      srDecimal: 0,
      zc: 0,
      zcDecimal: 0,
      jy: 0,
      jyDecimal: 0
    }

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let date = new Date();
    let currentDate = tool.formatDate(date, 'yyyy-MM-dd');
    let d = currentDate.split('-');
    this.data.pickerDate.start = `${d[0]}-${d[1]}-01`;
    this.data.pickerDate.end = currentDate;
    this.setData({
      currentDate,
      pickerDate: this.data.pickerDate
    })
  },

  onShow: function () {
    this.setData({
      currentMonthBooking: {
        sr: 0,
        srDecimal: 0,
        zc: 0,
        zcDecimal: 0,
        jy: 0,
        jyDecimal: 0
      }
    });
    this.getBookingDataByDate();
    this.getBookingDataByMonth();
  },

  // 获取当天记账数据
  getBookingDataByDate() {
    wx.showLoading({
      title: '拼命加载中',
      mask: true
    })

    wx.cloud.callFunction({
      name: 'find_data',
      data: {
        date: this.data.currentDate,
        listName: 'booking'
      }
    }).then(res => {
      wx.hideLoading();
      console.log(res);
      let date = this.data.currentDate.split('-');
      date = date[1] + '月' + date[2] + '日';

      this.setData({
        currentDateMoney: {
          sr: 0,
          zc: 0
        }
      });


      res.result.data.forEach(v => {
        this.data.currentDateMoney[v.mainType.type] += Number(v.money);
        v.money = Number(v.money).toFixed(2);
      });

      for (let key in this.data.currentDateMoney) {
        this.data.currentDateMoney[key] = this.data.currentDateMoney[key].toFixed(2);
      }

      this.setData({
        currentBookingData: res.result.data,
        date,
        currentDateMoney: this.data.currentDateMoney
      })

      console.log(this.data.currentBookingData);

    }).catch(err => {
      wx.hideLoading();
      console.log(err);
    })
  },

  // 切换日期
  toggleCurrentDate(e) {
    console.log('e ==>', e);
    if (e.detail.value == this.data.currentDate) {
      return;
    }
    this.setData({
      currentDate: e.detail.value
    });
    this.getBookingDataByDate();
  },

  // 获取当月记账数据
  getBookingDataByMonth() {
    wx.showLoading({
      title: '拼命加载中',
      mask: true
    });

    wx.cloud.callFunction({
      name: 'find_data_by_month',
      data: {
        start: this.data.pickerDate.start,
        end: this.data.pickerDate.end
      }
    }).then(res => {
      wx.hideLoading();
      console.log(res);
      res.result.data.forEach(v => {
        this.data.currentMonthBooking[v.mainType.type] += Number(v.money);
      })

      this.data.currentMonthBooking.jy = this.data.currentMonthBooking.sr - this.data.currentMonthBooking.zc;

      let keys = ['sr', 'zc', 'jy'];
      keys.forEach(v => {
        let data = Number(this.data.currentMonthBooking[v]).toFixed(2).split('.');
        this.data.currentMonthBooking[v] = data[0];
        this.data.currentMonthBooking[v + 'Decimal'] = data[1];
      })

      this.setData({
        currentMonthBooking: this.data.currentMonthBooking
      });
    }).catch(err => {
      wx.hideLoading();
      console.log(err);
    })
  }
})